#include "bsp/bsp.h"
#include "adc.h"
#include "tim.h"
#ifdef CONFIG_SPI_DEV
#include "spi.h"
#include "bsp/drv8301.h"
#endif
#include "gpio.h"
#include "bsp/cpu_ticks.h"
#include "board.h"

void SystemClock_Config(void);
void MX_IWDG_Init(void);
void MX_DMA_Init(void);
void HALL_IrqHandler(void);
void board_gpio_init(void);
static s32 _brd_error = 0;
static void system_init(void) {
	HAL_Init();
	MX_IWDG_Init();
	SystemClock_Config();
	SystemCoreClockUpdate();
	__HAL_RCC_SYSCFG_CLK_ENABLE();
	__HAL_DBGMCU_FREEZE_TIM1();
	__HAL_DBGMCU_FREEZE_IWDG();
	cpu_ticks_enable();
}

static void board_init(void) {
	MX_DMA_Init();
#ifdef CONFIG_SPI_DEV
	spi_hw_init();
#endif
#ifdef CONFIG_DRV8301_GAIN
	if (!drv8301_init(CONFIG_DRV8301_GAIN)) {
		_brd_error |= BOARD_ERROR_DRV8301;
	}
#endif
	aux_timer_init();

	board_gpio_init();
}

void bsp_init(void) {
	system_init();
	board_init();
}

s32 board_get_error(void) {
	return _brd_error;
}

void board_gpio_init(void) {

}

